package com.syqc.videos.player

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.amap.api.maps.AMap
import com.amap.api.maps.MapsInitializer
import com.syqc.base.BaseRealFragment
import com.syqc.comlib.custom.MLoadingDialog
import com.syqc.monitor.databinding.IncludeMapMatchBinding

/**
 *@author  hfjs
 *@time   2024/12/31 14:48
 *@desc
 */
abstract class RealMapFragment<VB : ViewBinding>(
    private val type: Int,
    private val inflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : BaseRealFragment<VB>(type, inflater) {
    protected var mMap: AMap? = null
    private lateinit var mapBindingVB: IncludeMapMatchBinding
    private var isFirstLoad = true // 是否第一次加载


    override fun onDestroyView() {
        super.onDestroyView()
        if (isNotLoad()) return
        mapBindingVB.mMapView.onDestroy()
        isFirstLoad = true
    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            isFirstLoad = false
            // 将数据加载逻辑放到onResume()方法中
            initData()
            initEvent()

        }
        mapBindingVB.mMapView.onResume()
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        MapsInitializer.updatePrivacyShow(requireContext(), true, true)
        MapsInitializer.updatePrivacyAgree(requireContext(), true)
        mapBindingVB = IncludeMapMatchBinding.bind(binding.root)
        mapBindingVB.mMapView.onCreate(savedInstanceState)
        mMap = mapBindingVB.mMapView.map
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onPause() {
        super.onPause()
        mapBindingVB.mMapView.onPause()
    }

    override fun onDestroy() {
        if (isNotLoad()) return
        mMap?.clear()
        mMap = null

        MLoadingDialog.dismiss()
        super.onDestroy()

    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapBindingVB.mMapView.onSaveInstanceState(outState)
    }

}